package cn.com.aiouyi.controller.web;

import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.ArticleClass;
import cn.com.aiouyi.entity.NavigationPosition;
import cn.com.aiouyi.page.Filter;
import cn.com.aiouyi.page.Order;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.ArticleClassService;
import cn.com.aiouyi.service.ArticleService;
import cn.com.aiouyi.service.NavigationPositionService;
import cn.com.aiouyi.service.NavigationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller(value = "pageController")
@RequestMapping("/page")
public class PageController extends BaseController{

	@Resource(name = "articleServiceImpl")
	private ArticleService articleService;
	@Resource(name = "articleClassServiceImpl")
	private ArticleClassService articleClassService;
	@Resource(name = "navigationServiceImpl")
	private NavigationService navigationService;
	@Resource(name = "navigationPositionServiceImpl")
	private NavigationPositionService navigationPositionService;

	@GetMapping
	public String index(Long articleClassId, ModelMap model){
		if (articleClassId != null) {
			ArticleClass articleClass = articleClassService.find(articleClassId);
			if (articleClass != null) {
				model.put("articleClass", articleClass);
				model.put("articles", articleClass.getArticles());
			} else {
				model.put("articles", new ArrayList<Article>());
			}
		} else {
			model.put("articles", articleService.findAll());
		}
		return "/web/page";
	}

	@GetMapping("/pageNavi")
	public String pageNavi(ModelMap model){

		List<ArticleClass> list = articleClassService.findAll();
		model.put("articleClasses", list);

//		NavigationPosition navigationPosition = navigationPositionService.findByCode(NavigationPosition.Code.pageNavi);
//		if(navigationPosition == null){
//			throw new RuntimeException("顶部导航位不存在");
//		}
//		List<Filter> filters = new ArrayList<Filter>();
//		filters.add(Filter.eq("isDisplay", true));
//		filters.add(Filter.isNull("parent"));
//		filters.add(Filter.eq("navigationPosition.id", navigationPosition.getId()));
//		List<Order> orders = new ArrayList<Order>();
//		orders.add(Order.asc("seq"));
//		model.put("navigations", navigationService.findList(null, filters, orders));
		return "/web/include/pageNavi";
	}
	
}
